﻿/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("T_VIEW_CIM",Enums.ActionPermissionOptions.Search)]
 */

using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using System;
using System.Data;

namespace VOL.WebApi.Operator.Wms
{
    internal class T_VIEW_CIMOperator
    {
        internal static bool SynchronizeInventoriesOperator()
        {
            string sql;
            try
            {

                DateTime datetime = GetSynchronizeInventoriesDateStamp();


                sql = @" SELECT  T_GOOD_NAME,T_GOOD_CODE ,T_GOOD_TYPE ,T_GOOD_TYPE_CODE,T_GOOD_MODEL,T_GOOD_UNIT,T_GOOD_PRICE_CUR ,T_EXC_RATE ,T_PRICE_BASE,T_GOOD_TAX_RATE ,T_QUALITY ,T_QUALITY_UNIT ,T_GOOD_PRICE_UNIT ,T_GOOD_PRICE_UNIT_CODE,
                EXP1,T_IS_GOOD,T_GOOD_STATUE,T_GOOD_STATUE_CODE,T_QUALITY_STAND,T_GOOD_REMARK,T_CREATE_DATE,T_UPDATE_DATE,EXP4 FROM  dbo.VIEW_CIM where T_CREATE_DATE>='" + datetime + "'";
                //sql = @" SELECT  T_GOOD_NAME,T_GOOD_CODE ,T_GOOD_TYPE ,T_GOOD_TYPE_CODE,T_GOOD_MODEL,T_GOOD_UNIT,T_GOOD_PRICE_CUR ,T_EXC_RATE ,T_PRICE_BASE,T_GOOD_TAX_RATE ,T_QUALITY ,T_QUALITY_UNIT ,T_GOOD_PRICE_UNIT ,T_GOOD_PRICE_UNIT_CODE,
                //EXP1,T_IS_GOOD,T_GOOD_STATUE,T_GOOD_STATUE_CODE,T_QUALITY_STAND,T_GOOD_REMARK,T_CREATE_DATE,T_UPDATE_DATE,EXP4 FROM  dbo.VIEW_CIM  where 1=1
                //  and T_GOOD_TYPE = '锭类' 
                //  and [T_GOOD_MODEL] = 'SK45'   order by T_CREATE_DATE desc";
                DataTable dt = new DataTable();
                dt = PurchasePub.SqlQuery(sql);
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (!IsExistInventory(dt.Rows[i]["T_GOOD_CODE"].ToString()))
                        {
                            sql = @"insert into T_VIEW_CIM(guid,  T_GOOD_NAME,T_GOOD_CODE ,T_GOOD_TYPE ,T_GOOD_TYPE_CODE,T_GOOD_MODEL,T_GOOD_UNIT,T_GOOD_PRICE_CUR ,T_EXC_RATE ,T_PRICE_BASE,T_GOOD_TAX_RATE ,T_QUALITY ,T_QUALITY_UNIT ,T_GOOD_PRICE_UNIT ,T_GOOD_PRICE_UNIT_CODE,
EXP1,T_IS_GOOD,T_GOOD_STATUE,T_GOOD_STATUE_CODE,T_QUALITY_STAND,T_GOOD_REMARK,T_CREATE_DATE,T_UPDATE_DATE,EXP4 )";
                            string guid = Guid.NewGuid().ToString();
                            sql += " values('" + guid + "','" + dt.Rows[i]["T_GOOD_NAME"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_CODE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_TYPE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_TYPE_CODE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_MODEL"].ToString() + "',";

                            sql += "'" + dt.Rows[i]["T_GOOD_UNIT"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_PRICE_CUR"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_EXC_RATE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_PRICE_BASE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_TAX_RATE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_QUALITY"].ToString() + "',";

                            sql += "'" + dt.Rows[i]["T_QUALITY_UNIT"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_PRICE_UNIT"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_PRICE_UNIT_CODE"].ToString() + "',";

                            sql += "'" + dt.Rows[i]["EXP1"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_IS_GOOD"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_STATUE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_STATUE_CODE"].ToString() + "',";

                            sql += "'" + dt.Rows[i]["T_QUALITY_STAND"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_GOOD_REMARK"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_CREATE_DATE"].ToString() + "',";

                            sql += "'" + dt.Rows[i]["T_UPDATE_DATE"].ToString() + "',";
                            sql+="'"+dt.Rows[i]["EXP4"].ToString() +"')";
                           int r = WmsPub.SqlExecute(sql);
                            if (r < 1)
                            {

                            }
                        }

                    }
                }
                return true;
            }
            catch

            {
                return false;
            }
        }

        private static bool IsExistInventory(string? T_GOOD_CODE)
        {
            if (T_GOOD_CODE == "")
            {
                return false;
            }
            else
            {
                string sql;
                sql = "select T_GOOD_CODE from [T_VIEW_CIM] where T_GOOD_CODE='" + T_GOOD_CODE + "'  ";
                DataTable tb = WmsPub.SqlQuery(sql);
                if (tb != null)
                {
                    if (tb.Rows.Count > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
                else
                {
                    return false;
                }
            }
        }

        private static DateTime GetSynchronizeInventoriesDateStamp()
        {
            string sql;
            sql = "select  isnull(max(T_CREATE_DATE),'') from [T_VIEW_CIM]   ";
            DataTable tb = WmsPub.SqlQuery(sql);
            if (tb != null)
            {
                if (tb.Rows[0][0].ToString() != "")
                {
                    return DateTime.Parse(tb.Rows[0][0].ToString());
                }
                else
                {
                    return DateTime.Parse("2001-01-01");
                }

            }
            else
            {
                return DateTime.Parse("2001-01-01");
            }


        }
    }
}